var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_defult:false,
    area:'',
    address:'',
    is_default:'',
    name:'',
    phone:'',
    is_edit:'',
    id:''
  },
  changeCb(){
    this.setData({
      is_default:!this.data.is_default
    })
  },
  choolAddress() {
    let that = this;
    wx.chooseLocation({
      success(res) {
        console.log(res);
        let address = res.address;
        let regex ="(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        console.log('匹配',address.match(regex));
        if(address.match(regex)){
          const provinces = address.match(regex)[1];
          const citys = address.match(regex)[2];
          const districts = address.match(regex)[3];
          const addressss = address.match(regex)[5]
          that.setData({
            area:provinces + "/" + citys + "/" + districts,
            address:addressss
          })
        
        }else{
          that.setData({
            area: address.substr(0,9),
            address:address.substr(9,(address.length-6))
          })
        }
        

      },
      fail(err) {
        wx.showToast({
          title: err
        })
      }
    })
  },
  submit(){
    if(!this.data.name){
      wx.showToast({
        title: '请填写姓名',
        icon:'none'
      })
      return false;
    }
    if(!this.data.phone){
      wx.showToast({
        title: '请填写电话',
        icon:'none'
      })
      return false;
    }
    if(!this.data.area){
      wx.showToast({
        title: '请填写地区',
        icon:'none'
      })
      return false;
    }
    if(!this.data.address){
      wx.showToast({
        title: '请填写详情地址',
        icon:'none'
      })
      return false;
    }
    let data = {
      id:this.data.id,
      name:this.data.name,
      phone:this.data.phone,
      area:this.data.area,
      address:this.data.address,
      is_default:this.data.is_default ? 1 : 0
    };
    api.post('/user/saveAddress',data).then(res=>{
      wx.setStorageSync('address', data)
      wx.navigateBack({
        delta:-1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.edit){
      this.setData({
        id:options.id,
        is_edit:1
      })
      let editAddress = wx.getStorageSync('editAddress');
      this.setData({
        address:editAddress.address,
        area:editAddress.area,
        name:editAddress.name,
        phone:editAddress.phone,
        is_default:editAddress.is_default
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})